<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 10:46
 */

namespace app\index\server;

use app\index\model\Image as ImageModel;
use app\lib\exception\DatabaseException;
use think\Exception;

class ImageServer extends BaseServer
{
    public function getList($params = [])
    {
        $params = $this->prep_param($params);
        try {
            // 总条数
            $count = ImageModel::count();
            $list = ImageModel::order($params['sort'], $params['order'])
                ->limit($params['offset'], $params['limit'])
                ->select();
        } catch (Exception $e) {
            // 有些问题 应该抛出错误异常
            return setMsg(0, $e->getMessage());
        }
        return bootstrap_table_return($count, $list);
    }

    public function add($params)
    {
        // 验证 alias是否唯一
        $exists_alias = $this->check_unique($params['alias']);
        if($exists_alias) {
            return setMsg(0, '设置的图片别名已经存在');
        }
        $data = [
            'alias' => $params['alias'],
            'from' => $params['from'],
            'status' => $params['status']
        ];
        if($params['from'] == 1) {
            // 来自本地 上传图片
            $image = $this->upload_img($params['url'], 'banner');
            $data['url']  = $image;
        } else {
            $data['url'] = $params['url'];
        }
        try {
            $result = ImageModel::insert($data);
            if(!$result) {
                return setMsg(0, '新增图片信息失败');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '新增图片信息成功');
    }

    public function update($params)
    {
        $exists_alias = $this->check_unique($params['alias'], $params['primary_key']);
        if($exists_alias) {
            return setMsg(0, '设置的图片别名已经存在!');
        }
        $data = [
            'alias' => $params['alias'],
            'from' => $params['from'],
            'status' => $params['status']
        ];
        // 是否有上传图片
        if($params['from'] == 1 && isset($params['image'])) {
            // 来自本地 上传图片
            $image = $this->upload_img($params['url'], 'banner');
            $data['url']  = $image;
        } else {
            // 其他的来自公网
            $data['url'] = $params['url'];
        }
        try {
            $result = ImageModel::where('id', '=', $params['primary_key'])
                ->update($data);
            if(!$result) {
                return setMsg(0, '修改此图片信息失败');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此图片信息成功');
    }

    public function detail($id)
    {
        try {
            $detail = ImageModel::get($id);
            if(!$detail) {
                return setMsg(0, '没有查到此图片信息');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '查找成功', $detail);
    }

    public function changeStatus($id)
    {
        try {
            $info = ImageModel::where('id', $id)
                ->find();
            $info->status = abs(($info->status) - 1);
            $result = $info->save();
            if(!$result) {
                return setMsg(0, '修改此图片状态失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此图片状态成功');
    }

    protected function check_unique($alias, $id = '')
    {
        if(isset($id)) {
            // 更新
            $result = ImageModel::where('id', '<>', $id)
                ->where('alias', '=', $alias)
                ->find();
        } else {
            $result = ImageModel::where('alias', '=', $alias)
                ->find();
        }
        return $result;
    }
}